package com.douqu.game.main.server.entity;

import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.main.server.netty.NettyTCPServerHandler;
import io.netty.buffer.ByteBuf;

/**
 * @author: Gavin.
 * Description:
 * Date: 2018/1/16 0016 下午 6:44
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
public class WaitPlayer {

    private String account;

    private long endTime;

    private NettyTCPServerHandler loginHandler;

    private ByteBuf byteBuf;

    public WaitPlayer(String account, long endTime, NettyTCPServerHandler loginHandler, ByteBuf param)
    {
        this.account = account;
        this.endTime = endTime;
        this.loginHandler = loginHandler;
        this.byteBuf = param;
    }

    public void login()
    {
        if(loginHandler != null && byteBuf != null )
        {
            loginHandler.login(byteBuf, loginHandler.getConnection().getIp());
        }
    }

    public boolean isWaitEnd(){
        return DataFactory.currentTime >= endTime;
    }

    public boolean isWaiting()
    {
        return DataFactory.currentTime < endTime;
    }

    public int getWaitTime()
    {
        return (int) (endTime - DataFactory.currentTime);
    }


    public boolean isOpenConnection()
    {
        return loginHandler.getConnection().getChannel().isOpen();
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public long getEndTime() {
        return endTime;
    }

    public void setEndTime(long endTime) {
        this.endTime = endTime;
    }

    public NettyTCPServerHandler getLoginHandler() {
        return loginHandler;
    }

    public void setLoginHandler(NettyTCPServerHandler loginHandler) {
        this.loginHandler = loginHandler;
    }

    public ByteBuf getByteBuf() {
        return byteBuf;
    }

    public void setByteBuf(ByteBuf byteBuf) {
        this.byteBuf = byteBuf;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof WaitPlayer)) return false;

        WaitPlayer that = (WaitPlayer) o;

        if (!account.equals(that.account)) return false;

        return true;
    }


    @Override
    public int hashCode()
    {
        return account == null ? 0 : account.hashCode();
    }

}
